let pId = location.href.split("?")[1].split("=")[1];

console.log(pId);


new Vue({
    el: ".goodlist",
    data: {
        goodsInfo: {},
        num: 10
    },
    methods: {
        sub() {
            if (this.num == 1) return
            this.num--; //数据驱动页面
        },
        add() {
            this.num++;
        },
        //加入购物车
        joinCart() {
            // 购买人,产品信息,数量
            //给 u_id去了一个新的名字 uId
            let { u_id: uId } = JSON.parse(localStorage.getItem("user") || '{}');

            this.goodsInfo.u_id = uId;
            this.goodsInfo.num = this.num;

            let saveObj = {...this.goodsInfo };
            delete(saveObj.p_imgs)
                // console.log(saveObj);
                //通过ajax 把 saveObj 传输到后端

            $.ajax({
                type: 'post',
                url: "/api/addCart",
                data: saveObj,
                success(res) {
                    console.log(res);
                    layer.msg(res.msg)

                }
            })

        }
    },
    created: function() {
        let pId = location.href.split("?")[1].split("=")[1];
        let self = this;
        $.ajax({
            url: "/api/getByPid",
            data: {
                pId
            },
            success(res) {
                self.goodsInfo = res
            }
        })
    }
})


let vm = new Vue({
    el: "trTitle",
    data: {
        list: []
    }, //数据
    methods: { //方法 
        //减法
        sub(obj) {
            if (obj.p_number == 1) {
                return;
            }
            obj.p_number--;

            obj.p_total = obj.p_number * obj.p_price;

            let { c_id, p_number: num } = obj;
            $.ajax({
                url: "/api/update",
                type: "post",
                data: {
                    c_id,
                    num
                }
            }).then(function(res) {
                layer.msg(res.msg)
            })
        }
        //加法
        ,
        sup(obj) {
            obj.p_number++;
            obj.p_total = obj.p_number * obj.p_price;

            let { c_id, p_number: num } = obj;

            $.ajax({
                url: "/api/update",
                type: "post",
                data: {
                    c_id,
                    num
                }
            }).then(function(res) {
                layer.msg(res.msg)
            })

        }
        //删除
        ,
        del_cart(c_id, delete_index) {
            let self = this;
            layer.confirm("确定要删除吗?亲..要不要再考虑一下下", {
                btn: ["yes", "no"]
            }, function(index) {
                layer.close(index);

                // self.list.splice(index_, 1);

                // console.log(self.list);
                $.ajax({
                    url: "/api/delete",
                    type: "post",
                    data: {
                        c_id
                    },
                    success(res) {
                        layer.msg(res.msg);

                        if (res.status == 1) {
                            // var index = self.list.findIndex(el => el.c_id == c_id);
                            self.list.splice(delete_index, 1);
                        }
                    }
                })
            })
        }
    },
    //钩子函数(自动触发) created
    created: function() {
        let self = this;
        let { u_id: userId } = JSON.parse(localStorage.getItem("user") || '{}');
        console.log(userId);
        $.ajax({
            url: "/api/cartlist?tmp=" + Date.now(),
            data: { userId },
            success(res) {
                console.log(res);
                self.list = res.data
            }
        })
    }


})